# 爬取图片

import time
import requests
from bs4 import BeautifulSoup

#彼岸桌面网站
#解析网页批量下载图片
class Aaa():
    #设置请求Cookie，User-Agent抓包可查看到
    headers = {
        "Cookie": "__cfduid=dfeaf16009458cb626353011331e826a71604114742",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
    }

    def get_cookies(self):
        url = "http://pic.netbian.com"
        response = requests.get(url=url)
        self.headers ={
            "Cookie":"__cfduid=" + response.cookies["__cfduid"],
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
        }

    # 获取图片列表
    def get_image_list(self,url):
        try:
            response = requests.get(url=url,headers=self.headers)
            response.encoding = 'gbk'
            soup = BeautifulSoup(response.text,'lxml')#页面内容
            li_list = soup.select("#main > div.slist > ul.clearfix > li")#获取图片li标签集合
            for li in li_list:
                href = "http://pic.netbian.com" + li.select_one("img").attrs["src"]#获取标签连接
                self.get_image_src(href)#直接调用下载
        except:
            self.get_cookies()


    def get_image(self,href):
        try:
            response = requests.get(url=href,headers=self.headers)
            response.encoding = 'gbk'
            soup = BeautifulSoup(response.text, 'lxml')
            image_href = "http://pic.netbian.com" + soup.select_one("#main > div.endpage > div > p > a").attrs["href"]
            self.get_image_src(image_href)
        except:
            self.get_cookies()


    def get_image_src(self,href):
        try:
            # response = requests.get(url=href,headers=self.headers)
            # response.encoding = 'gbk'
            # soup = BeautifulSoup(response.text, 'lxml')
            # src = soup.select("img")[1].attrs["src"]
            print(href)
            self.download_image(href)
        except:
            self.get_cookies()

    # 下载图片
    def download_image(self,image_src):
        try:
            title = str(time.time()).replace('.', '')
            image_path = "D:/image/" + title + ".png",
            image_path = list(image_path)
            response = requests.get(image_src,headers=self.headers)
            # 获取的文本实际上是图片的二进制文本
            img = response.content
            # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
            with open(image_path[0],'wb' ) as f:
                print("下载图片："+title)
                f.write(img)
        except:
            self.get_cookies()

if __name__ == '__main__':
    aaa = Aaa()
    aaa.get_cookies()
    # aaa.get_image_list("http://youjiahu.cn/image/home")
    for i in range(1,200):
        url = "http://pic.netbian.com/4kmeinv/index_{}.html".format(i)
        aaa.get_image_list(url)
        # time.sleep(10)http://pic.netbian.com/uploads/allimg/170705/232057-1499268057c1ad.jpg